﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MarioArena.World;

namespace MarioArena.Animation
{
    public class AnimationSet
    {
        public int FramesPerSecond { get; set; }
        public Color AnimationColor { get; set; }

        private SpriteAnimation mPassiveAnimation;
        private SpriteAnimation mRunAnimation;
        private SpriteAnimation mJumpAnimation;
        private SpriteAnimation mFallAnimation;

        /// <summary>
        /// Constructs a new set of animations.
        /// </summary>
        /// <param name="passiveAnimation">SpriteAnimation to be displayed when the character is passive.</param>
        /// <param name="runAnimation">SpriteAnimation to be displayed when the character is running.</param>
        /// <param name="jumpAnimation">SpriteAnimation to be displayed when the character is jumping.</param>
        /// <param name="fallAnimation">SpriteAnimation to be displayed when the character is falling.</param>
        public AnimationSet(SpriteAnimation passiveAnimation, SpriteAnimation runAnimation, SpriteAnimation jumpAnimation, SpriteAnimation fallAnimation)
        {
            this.mPassiveAnimation = passiveAnimation;
            this.mRunAnimation = runAnimation;
            this.mJumpAnimation = jumpAnimation;
            this.mFallAnimation = fallAnimation;

            this.FramesPerSecond = 9;
            this.AnimationColor = Color.White;
        }

        /// <summary>
        /// Updates the AnimationCollection.
        /// </summary>
        /// <param name="playerState">The current state of the character.</param>
        public void Update(GameTime gameTime, PlayerState playerState)
        {
            if (playerState == PlayerState.Passive)
            {
                this.mPassiveAnimation.Update(gameTime);
            }
            else if (playerState == PlayerState.Running)
            {
                this.mRunAnimation.Update(gameTime);
            }
            else if (playerState == PlayerState.Jumping)
            {
                this.mJumpAnimation.Update(gameTime);
            }
            else if (playerState == PlayerState.Falling)
            {
                this.mFallAnimation.Update(gameTime);
            }
        }

        /// <summary>
        /// Draws the AnimationCollection, drawing the correct animation depending on the playerstate.
        /// </summary>
        /// <param name="position">The position of the animation.</param>
        /// <param name="playerState">The current state of the character.</param>
        public void Draw(SpriteBatch spriteBatch, Vector2 position, PlayerState playerState)
        {
            if (playerState == PlayerState.Passive)
            {
                this.mPassiveAnimation.framesPerSecond = this.FramesPerSecond;
                this.mPassiveAnimation.color = this.AnimationColor;
                this.mPassiveAnimation.position = position;
                this.mPassiveAnimation.Draw(spriteBatch, position);
            }
            else if (playerState == PlayerState.Running)
            {
                this.mRunAnimation.framesPerSecond = this.FramesPerSecond;
                this.mRunAnimation.color = this.AnimationColor;
                this.mRunAnimation.position = position;
                this.mRunAnimation.Draw(spriteBatch, position);
            }
            else if (playerState == PlayerState.Jumping)
            {
                this.mJumpAnimation.framesPerSecond = this.FramesPerSecond;
                this.mJumpAnimation.color = this.AnimationColor;
                this.mJumpAnimation.position = position;
                this.mJumpAnimation.Draw(spriteBatch, position);
            }
            else if (playerState == PlayerState.Falling)
            {
                this.mFallAnimation.framesPerSecond = this.FramesPerSecond;
                this.mFallAnimation.color = this.AnimationColor;
                this.mFallAnimation.position = position;
                this.mFallAnimation.Draw(spriteBatch, position);
            }
        }

        /// <summary>
        /// Flips the animation collection horizontally.
        /// </summary>
        public void FlipHorizontally()
        {
            this.mPassiveAnimation.spriteEffect = SpriteEffects.FlipHorizontally;
            this.mRunAnimation.spriteEffect = SpriteEffects.FlipHorizontally;
            this.mJumpAnimation.spriteEffect = SpriteEffects.FlipHorizontally;
            this.mFallAnimation.spriteEffect = SpriteEffects.FlipHorizontally;
        }

        /// <summary>
        /// Removes the flip effect from the animation collection.
        /// </summary>
        public void UnFlipHorizontally()
        {
            this.mPassiveAnimation.spriteEffect = SpriteEffects.None;
            this.mRunAnimation.spriteEffect = SpriteEffects.None;
            this.mJumpAnimation.spriteEffect = SpriteEffects.None;
            this.mFallAnimation.spriteEffect = SpriteEffects.None;
        }
    }
}
